<template>
	<view style="padding: 20rpx;box-sizing: border-box;">
		<u-toast ref="uToast"></u-toast>
		<u--form labelPosition="left" labelAlign="center" :model="evaluateForm" :rules="evaluateFormRules"
			ref="evaluateForm" labelWidth="70">
			<u-form-item label="订单号" prop="number" borderBottom>
				<u--input v-model="evaluateForm.number" disabled border="none"></u--input>
			</u-form-item>
			<u-form-item>
				<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" name="uploadOrderImage"
					multiple :maxCount="9"></u-upload>
			</u-form-item>
			<u-form-item label="综合评分" borderBottom>
				<uni-rate v-model="evaluateForm.rate" :size="28" :margin="8"/>
			</u-form-item>
			<u-form-item prop="comment">
				<u--textarea placeholder="请多多描述打印情况和使用感受" v-model="evaluateForm.comment" count></u--textarea>
			</u-form-item>
		</u--form>
		<u-button type="primary" text="提交评价" customStyle="margin-top: 20px" @click="commit"></u-button>
	</view>
</template>

<script>
	import {
		ptUrl
	} from '@/utils/env.js'
	export default {
		data() {
			return {
				ptUrl: ptUrl,
				evaluateForm: {
					orderId: null,
					number: null,
					comment: "",
					rate: 5,
				},
				evaluateFormRules: {
					'number': {
						type: 'string',
						required: true,
						message: '订单号不能为空',
						trigger: ['change']
					},
					'rate': {
						type: 'number',
						required: true,
						message: '综合评分不能为空',
						trigger: ['blur', 'change']
					},
					'comment': {
						type: 'string',
						required: true,
						message: '必填,请您填写订单评价',
						trigger: ['blur', 'change']
					},
				},
				fileList: [],
				toast: {
					type: 'success',
					title: '评价成功',
					message: "评价成功",
					iconUrl: '@/static/success.png',
					url: '/pages/order/order',
				},
			}
		},
		onLoad(options) {
			if (options.data) {
				console.log(options.data)
				const data = JSON.parse(options.data)
				this.evaluateForm.orderId = data.orderId
				this.evaluateForm.number = data.number
			}
		},
		onReady() {
			this.$refs.evaluateForm.setRules(this.evaluateFormRules)
		},
		methods: {
			async afterRead(event) {
				this.fileList = this.fileList.concat(event.file)
			},
			deletePic(event) {
				this.fileList.splice(event.index, 1)
			},
			commit() {
				console.log("commit")
				this.$refs.evaluateForm.validate().then(() => {
					uni.showModal({
						title: '提交评价',
						content: '是否确认提交评价?',
						success: res => {
							if (res.confirm) {
								uni.showLoading({
									title: '提交中！'
								});
								this.addEvaluate()
							} 
						}
					});
				}).catch(errors => {
					uni.$u.toast(errors[0].message)
				})
			},
			async addEvaluate(){
				console.log("addEvaluate")
				let image = []
				for (let i = 0; i < this.fileList.length; i++) {			
					const result = await this.uploadFilePromise(this.fileList[i].url);
					image.push(result);
				}
				let data = JSON.parse(JSON.stringify(this.evaluateForm))
				data.image = JSON.stringify(image);
				console.log(data)
				this.$http("/evaluate/addEvaluate","POST",data).then(()=>{
					this.showToast()
				})
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					uni.uploadFile({
						url: ptUrl + '/evaluate/uploadEvaluateImage',
						name: "file",
						filePath: url,
						header: {
							'Authorization': uni.getStorageSync('token'),
						},
						success: (res) => {
							let data = JSON.parse(res.data)
							resolve(data.data)
						},
						fail(err) {
							reject(err)
						},
					});
				});
			},
			showToast() {
				uni.hideLoading()
				let params = this.toast
				this.$refs.uToast.show({
					...params,
					complete() {
						params.url && uni.switchTab({
							url: params.url
						})
					}
				})
			},
		}
	}
</script>

<style>
</style>
